define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/userbox/index',
                    add_url: 'box/userbox/add',
                    edit_url: 'box/userbox/allot',
                    del_url: 'box/userbox/del',
                    multi_url: 'box/userbox/multi',
                    table: 'user_box',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_box_id',
                sortName: 'user_box_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                queryParams: function (params) {
//                        var options = table.bootstrapTable('getOptions');
                    //这里可以追加搜索条件
//                        var filter = JSON.parse(params.filter);
//                        var op = JSON.parse(params.op);
//                        filter.referee_id=Fast.api.query('ref');
//                        params.filter = JSON.stringify(filter);
//                        params.op = JSON.stringify(op);
//                        return params;
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.status = 0;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'apply_no', title: __('Apply_no')},
                        {
                            field: 'city_info',
                            title: __('所属区域'),
                            formatter: Controller.api.formatter.city_info,
                            align: 'left'
                        },

                        {
                            field: 'house_info',
                            title: __('所属小区'),
                            formatter: Controller.api.formatter.house_info,
                            align: 'left'
                        },
                        {field: 'contacts', title: __('Contacts')},
                        {field: 'sex_text', title: __('Sex')},
                        {field: 'telephone', title: __('Telephone')},
                        {field: 'remark', title: __('Remark')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'audit_time', title: __('Audit_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'box_no', title: __('Box_no')},
                        {field: 'refusal_time', title: __('Refusal_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'refusal_reason', title: __('Refusal_reason')},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });
            $('#table').bootstrapTable('hideColumn', 'audit_time');
            $('#table').bootstrapTable('hideColumn', 'box_no');
            $('#table').bootstrapTable('hideColumn', 'refusal_time');
            $('#table').bootstrapTable('hideColumn', 'refusal_reason');

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {


                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (typeStr == 'one') {
                        $('#table').bootstrapTable('hideColumn', 'audit_time');
                        $('#table').bootstrapTable('hideColumn', 'box_no');
                        $('#table').bootstrapTable('hideColumn', 'refusal_time');
                        $('#table').bootstrapTable('hideColumn', 'refusal_reason');
                        $('#table').bootstrapTable('showColumn', 'custom');
                        $('#fenpei').show();
                        //如果希望追加搜索条件,可使用
                        filter.status = 0;
                    }

                    if (typeStr == 'two') {
                        //如果希望追加搜索条件,可使用
                        $('#table').bootstrapTable('showColumn', 'audit_time');
                        $('#table').bootstrapTable('showColumn', 'box_no');
                        $('#table').bootstrapTable('hideColumn', 'refusal_time');
                        $('#table').bootstrapTable('hideColumn', 'refusal_reason');
                        $('#table').bootstrapTable('hideColumn', 'custom');
                        $('#fenpei').hide();
                        filter.status = 1;

                    }

                    if (typeStr == 'three') {
                        //如果希望追加搜索条件,可使用
                        $('#table').bootstrapTable('showColumn', 'refusal_time');
                        $('#table').bootstrapTable('showColumn', 'refusal_reason');
                        $('#table').bootstrapTable('hideColumn', 'audit_time');
                        $('#table').bootstrapTable('hideColumn', 'box_no');
                        $('#table').bootstrapTable('hideColumn', 'custom');
                        $('#fenpei').hide();
                        filter.status = 10;

                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $(table).on("click", "[data-id].btn-refuse", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('拒绝原因'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: url + "?user_box_id=" + id + "&refusal_reason=" + value,
                        data: $(that).closest("form").serialize()
                    }, function () {
                        table.bootstrapTable('refresh', {});
                    });

                    Layer.close(index);
                });
            });
            // 分配盒子
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table);

                $.post('box/userbox/checkids', {ids: ids.join(",")}, function (res) {
                    if (res.code == 1) {
                        Backend.api.open('box/userbox/allot/ids/' + ids.join(","), __('分配盒子'), {area: ["90%", "90%"]}, {
                            callback: function (value) {
                                table.bootstrapTable('refresh', {});
                            },
                        });
                    } else {
                        Layer.alert(res.msg);
                        return;
                    }

                }
                );

            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        }, allot: function () {
            var table2 = $("#table");
            var ids = table2.attr('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/userbox/goodslist?ids=' + ids,
                    add_url: 'box/userbox/add',
                    edit_url: 'box/userbox/allot',
                    del_url: 'box/userbox/del',
                    multi_url: 'box/userbox/multi',
                    table: 'user_box',
                }
            });



            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                toolbar: '#toolbar1',
                showRefresh: true,
                commonSearch: false,
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'), formatter: Controller.api.formatter.goods_info, operate: false, align: 'left'},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'goods_price', title: __('Goods_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: false},
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {
                            field: 'stock_num',
                            title: __('Stock_num'),
                            editable: {
                                type: 'text'
                            }
                        },
                        {field: 'createtime', title: __('Goods_createtime'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'goods_custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.goods_custom
                        },
                    ]
                ],
                onEditableSave: function (field, row, oldValue, $el) {
                    $.ajax({
                        type: "post",
                        url: "box/userbox/editstock?ids=" + ids,
                        data: row,
                        dataType: 'JSON',
                        success: function (data, status) {
                            if (status == "success") {
                                Toastr.success('操作成功');
                            }
                        },
                        error: function () {
                            Toastr.success('编辑失败');
                        },
                    });
                }
            });

            $(document).on("click", "[data-id].btn-refresh", function (e) {
                table2.bootstrapTable('refresh', {});
            });

            $(document).on("click", ".btn-addgoods", function (e) {
                Backend.api.open('box/userbox/addgoods?ids=' + ids, __('添加盒子商品'), {
                    callback: function (value) {
                        table2.bootstrapTable('refresh', {});
                    }
                });
            });

            $(table2).on("click", "[data-id].btn-delgoods", function (e) {
                e.preventDefault();
                var id = $(this).data("id");

                var that = this;
                Layer.confirm(
                        __('确定删除盒子商品吗？'),
                        {icon: 3, title: __('确定删除盒子商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('box/userbox/delgoods', {id: id, ids: ids}, function (res) {
                                table2.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                //这里是表单提交处理成功后的回调函数，接收来自php的返回数据
                Fast.api.close(data);//这里是重点
            }, function (data, ret) {
            });

        }, addgoods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/userbox/addgoods/index?ids=' + Fast.api.query('ids'),
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'goods',
                }
            });

            var table1 = $("#table");
            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                queryParams: function (params) {
//                        var options = table.bootstrapTable('getOptions');
                    //这里可以追加搜索条件
//                        var filter = JSON.parse(params.filter);
//                        var op = JSON.parse(params.op);
//                        filter.referee_id=Fast.api.query('ref');
//                        params.filter = JSON.stringify(filter);
//                        params.op = JSON.stringify(op);
//                        return params;
                    var options = table1.bootstrapTable('getOptions');
                    options.pageNumber = 1;
                    var filter = JSON.parse(params.filter);
                    filter.status = 1;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'), formatter: Controller.api.formatter.goods_info, operate: 'like', align: 'left'},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'cate_id', title: __('Cate_title'), visible: false, searchList: $.getJSON('goods/cateList')},
                        {field: 'brand_id', title: __('Brand_title'), visible: false, searchList: $.getJSON('goods/brandList')},
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'add_goods_custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.add_goods_custom
                        },
                    ]
                ]
            });



            $(table1).on("click", "[data-id].btn-addboxgoods", function (e) {
                e.preventDefault();


                var id = $(this).data("id");
                var ids = $(this).data("ids");
                $.post('box/userbox/addboxgoods', {goods_id: id, ids: ids}, function (res) {
                    table1.bootstrapTable('refresh', {});
                });
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                city_info: function (value, row, index) {
                    var html = row.province_name + '-' + row.city_name + '-' + row.area_name;
                    return html;
                },
                goods_info: function (value, row, index) {
                    var html = row.goods_title + ',' + row.goods_model + ',' + row.goods_spec;
                    return html;
                },
                house_info: function (value, row, index) {

                    var html = '<a href="box/userbox/laying?rid=' + row.room_id + '" class="btn btn-info btn-xs btn-add btn-dialog" title="房间布放记录" data-table-id="table">' + row.quarters_name + '-' + row.unit_name + '-' + row.room_name + '</a>';
                    return html;
                },
                add_goods_custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a class="text-success btn-addboxgoods"  style="cursor:pointer" data-url="box/userbox/addboxgoods" data-ids="' + Fast.api.query('ids') + '" data-id="' + row.goods_id + '"><b>添加</b></a>';

                    return html;
                },
                goods_custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '';
                    html += '<a class="text-danger btn-delgoods"  style="cursor:pointer" data-url="box/userbox/delgoods" data-id="' + row.goods_id + '"><b>删除</b></a>';

                    return html;
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '';
                    html += '<a class="text-danger btn-refuse"  style="cursor:pointer" data-url="box/userbox/checkno" data-id="' + row.user_box_id + '"><b>拒绝申请</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});